﻿using System;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using FrameWork.Lib;
using FrameWork.Lib.DataObjects;
using REVISIONMAX30.DataObjects;
using FrameWork.WinUI.Common;
using FrameWork.WinUI.Controls;

namespace REVISIONMAX30
{
    public class UIManager : UIManagerBase
    {
        public frmGroupList GroupListPanel = new frmGroupList();
        public DataObjects.Data Data { get; private set; }


        public UIManager(DockPanel pDockPanel, EventManager pEventManager)
            : base(pDockPanel, pEventManager)
        {

        }

        public void InitData(DataObjects.Data pData)
        {
            this.Data = pData;
            this.GroupListPanel.Init(pData);
        }

        public EventManager EventManager
        {
            get
            {
                return (EventManager)base.EventManagerBase;
            }
        }

        ~UIManager()
        {
            this.GroupListPanel.Close();
            this.GroupListPanel.Dispose();
            this.GroupListPanel = null;
        }

        public void InitGroupPanel()
        {
            this.GroupListPanel.MdiParent = this.ParentForm;
            this.GroupListPanel.Show(this.DockPanel);
            this.GroupListPanel.UIManager = this;
            this.GroupListPanel.DockState = DockState.DockLeft;
            this.GroupListPanel.VisibleState = DockState.DockLeft;
        }

        public string ShowHideGroups()
        {
            if (this.GroupListPanel.Visible)
            {
                this.GroupListPanel.Hide();
            }
            else
            {
                this.GroupListPanel.Show();
            }

            return this.GroupListPanel.Visible ? "Hide Groups Pane" : "Show Groups Pane";
        }


        public void ViewConnectionString(DataObjects.ConnectionString pConnectionString, DataObjects.Group pGroup)
        {
            new frmConnectionString().View(pConnectionString, pGroup);
        }

        public void ViewGroup(DataObjects.Group pGroup, DataObjects.Data pParent)
        {
            frmGroup tFrmGroup = null;

            foreach (var tDockPane in this.DockPanel.Panes)
            {
                foreach (var tDockContent in tDockPane.Contents)
                {
                    if ((tDockContent is frmGroup) && (((frmGroup)tDockContent).DataObjectGUID == pGroup.DataObjectGUID))
                    {
                        tFrmGroup = (frmGroup)tDockContent;
                        tFrmGroup.BringToFront();
                        break;
                    }
                }
            }

            if (tFrmGroup == null)
            {
                tFrmGroup = new frmGroup();
                tFrmGroup.MdiParent = this.ParentForm;
                tFrmGroup.Show(this.DockPanel);
                tFrmGroup.DockState = DockState.Document;
                tFrmGroup.VisibleState = DockState.Document;
                tFrmGroup.UIManager = this;
                tFrmGroup.View(pGroup, pParent);
            }
        }

        public void ViewSourceDirectory(DataObjects.SourceDirectory pSourceDirectory, DataObjects.Group pGroup)
        {
            var t = new frmSourceDirectory();
            t.UIManager = this;
            t.View(pSourceDirectory, pGroup);
        }

        public void ViewSQLItem(DataObjects.SQLItem pSQLItem, DataObjects.Group pGroup)
        {
            var t = new frmSQLItem();
            t.UIManager = this;
            t.View(pSQLItem, pGroup);
        }

    }
}

